<template>
  <div class="table-demo-container layout-padding">
    <div class="table-demo-padding layout-padding-view layout-padding-auto">
      <TableSearch :search="state.tableData.search" @search="onSearch"/>
      <Table
          ref="tableRef"
          v-bind="state.tableData"
          class="table-demo"
          @delRow="onTableDelRow"
          @pageChange="onTablePageChange"
          @sortHeader="onSortHeader"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import {defineAsyncComponent, reactive, ref, onMounted} from 'vue';
import {ElMessage} from 'element-plus';
import {orderApi} from "/@/api/order";

// 引入组件
const Table = defineAsyncComponent(() => import('/@/components/table/index.vue'));
const TableSearch = defineAsyncComponent(() => import('/@/views/pages/order/search.vue'));

// 定义变量内容
const tableRef = ref<RefType>();
const state = reactive<TableDemoState>({
  hadVal: false,
  tableData: {
    // 列表数据（必传）
    data: [],
    // 表头内容（必传，注意格式）
    header: [
      {key: 'order_sn', colWidth: '', title: '订单号', type: 'text', isCheck: true},
      {key: 'user_id', colWidth: '', title: '周期', type: 'text', isCheck: true},
      {key: 'price_total', colWidth: '', title: '订单金额', type: 'text', isCheck: true},
      {key: 'pay_total', colWidth: '', title: '实际支付', type: 'text', isCheck: true},
      {key: 'status', colWidth: '', title: '订单状态', type: 'text', isCheck: true},
      {key: 'created_at', colWidth: '', title: '下单时间', type: 'text', isCheck: true},
    ],
    // 配置项（必传）
    config: {
      total: 0, // 列表总数
      loading: true, // loading 加载
      isBorder: false, // 是否显示表格边框
      isSerialNo: true, // 是否显示表格序号
      isSelection: false, // 是否显示表格多选
      isOperate: true, // 是否显示表格操作栏
    },
    // 搜索表单，动态生成（传空数组时，将不显示搜索，注意格式）
    search: [
      {label: '商品名称', prop: 'goods_name', placeholder: '请输入商品名称', required: false, type: 'input'},
      {label: '订单编号', prop: 'order_sn', placeholder: '请输入订单编号', required: false, type: 'input'},
      {label: '下单时间', prop: 'time', placeholder: '请选择', required: false, type: 'date'},
    ],
    param: {
      pageNum: 1,
      pageSize: 10,
    },
  },
});

// 初始化列表数据
const getTableData = async () => {
  state.tableData.config.loading = true;
  state.tableData.data = [];
  const res = await orderApi().lists(state.tableData.param);
  state.tableData.config.loading = false;
  if (res.code != 0) {
    return;
  }
  state.tableData.data = res.data.list;
  // 数据总数（模拟，真实从接口取）
  if (!state.hadVal) {
    state.tableData.config.total = res.data.count;
    state.hadVal = true;
  }

};
// 搜索点击时表单回调
const onSearch = (data: EmptyObjectType) => {
  state.hadVal = false;
  state.tableData.param = Object.assign({}, state.tableData.param, {...data});
  tableRef.value.pageReset();
};
// 删除当前项回调
const onTableDelRow = (row: EmptyObjectType) => {
  ElMessage.success(`删除${row.name}成功！`);
  getTableData();
};
// 分页改变时回调
const onTablePageChange = (page: TableDemoPageType) => {
  state.tableData.param.pageNum = page.pageNum;
  state.tableData.param.pageSize = page.pageSize;
  getTableData();
};
// 拖动显示列排序回调
const onSortHeader = (data: TableHeaderType[]) => {
  state.tableData.header = data;
};
// 页面加载时
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">
.table-demo-container {
  .table-demo-padding {
    padding: 15px;

    .table-demo {
      flex: 1;
      overflow: hidden;
    }
  }
}
</style>
